package cn.bluarry.sqlitetest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.nio.DoubleBuffer;

public class MainActivity extends AppCompatActivity {
    private MyDataBaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn=findViewById(R.id.btn);
        dbHelper=new MyDataBaseHelper(this,"Book.db",null,2);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
            }
        });

        Button add_btn=findViewById(R.id.add_btn);
        add_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db=dbHelper.getWritableDatabase();
                ContentValues vv=new ContentValues();
                vv.put("name","书1");
                vv.put("author","aaaa");
                vv.put("pages",123);
                vv.put("price",324.123);
                db.insert("Book",null,vv);
                vv.clear();
                vv.put("name","书2");
                vv.put("author","bbbb");
                vv.put("pages",432);
                vv.put("price",111.345);
                db.insert("Book",null,vv);
                vv.clear();
            }
        });

        Button update=findViewById(R.id.update_btn);
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db= dbHelper.getWritableDatabase();
                ContentValues vv=new ContentValues();
                vv.put("price",12.12);
                db.update("Book",vv,"id= ? AND name= ?",new String[]{"3","书1"});
            }
        });

        Button delete=findViewById(R.id.delete_btn);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db=dbHelper.getWritableDatabase();

                db.delete("Book","id=?",new String[]{"4"});

            }
        });

        Button query=findViewById(R.id.query_btn);
        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db=dbHelper.getWritableDatabase();
                Cursor cursor=db.query("Book",null,null,null,null,null,null);
                if(cursor.moveToFirst()){
                    do{
                        Log.d("QueryResult", " bookname="+cursor.getString(cursor.getColumnIndex("name"))+
                                                        " author= "+cursor.getString(cursor.getColumnIndex("author"))
                                                        +" price= "+ cursor.getDouble(cursor.getColumnIndex("price"))
                                                        +" pages=" + cursor.getInt(cursor.getColumnIndex("pages"))
                                                        );
                    }while(cursor.moveToNext());
                }
                cursor.close();
            }
        });


    }

}
